function enter(action) {
	$("#field-"+action).blur();
	changeField(action);
}

$(document).delegate('.field', 'focus', function() {
	// on focus add class
	$(this).addClass("focus");
});

$(document).delegate('.field', 'blur', function() {
	// on focus remove class
	$(this).removeClass("focus");
	changeField($(this).attr("id").substring(6));
});

function changeField(action) {
	// update profile field
	value = $("#field-"+action).val();
	$.ajax({
			type:"POST",
			url:$("#project-base").val()+'Users',
			async: true,
			data: "action="+action+"&value="+value,
			success:function(msg) {
				$("#field-"+action+"-error").html("");
			},
			error:function(msg) {
				$("#field-"+action+"-error").html("X");
			}
		});
}

function togglePasswordBox() {
	// show password box
	$(".password-change").slideToggle();
	$("#field-old-password").val("");
	$("#field-new-password").val("");
	$("#field-re-password").val("");
	$("#password-error").html("");
}

function changePassword() {
	// change password in db
	data = $(".password-change").serialize();
	if (($("#field-re-password").val() == "") || ($("#field-new-password").val() == "")) {
		// check that new password filled out
		$("#password-error").html("Missing new password");
		return;
	}
	if ($("#field-new-password").val() != $("#field-re-password").val()){
		// if new password is same as old password do nothing
		$("#password-error").html("Passwords don't match");
		return;
	}
	$.ajax({
		type:"POST",
		url:$("#project-base").val()+'Users',
		async: true,
		data: data,
		success:function(msg) {
			$("#password-error").html("");
			togglePasswordBox();
		},
		error:function(msg) {
			$("#password-error").html(msg.responseText);
		}
	});
}